﻿Imports System.DirectoryServices.AccountManagement
Imports System.IO

Public Class ClientValidation
    Public StrFile As String
    Public StrLogFile As String = ModGlobals.StrLogFile

    'Builds PrincipalContext not currently using
    Public Function GetPrincipalContext() As PrincipalContext
        Dim oPrincipalContext As New PrincipalContext(ContextType.Domain, Nothing, Nothing, ContextOptions.SimpleBind, Nothing, Nothing)
        Return oPrincipalContext
    End Function
    'Validates user credentials
    Public Function ValidateCredentials(sUserName As String, sPassword As String) As Boolean
        Dim oPrincipalContext As New PrincipalContext(ContextType.Domain, Nothing, Nothing, ContextOptions.SimpleBind, Nothing, Nothing)
        Return oPrincipalContext.ValidateCredentials(sUserName, sPassword)
    End Function
    Public Function ValidateCredentialsLog() As boolean
        Dim booleanVal As boolean = False
        Dim dtmStartTime As DateTime = CType(DateTime.Now.ToString("G"),DateTime)
        MsgBox("Incorrect Username or password entered!")
        WriteLog("NetDomTool Rename Report:")
        WriteLog("Runtime: " & dtmStartTime)
        WriteLog("Error: Incorrect Username or password entered!")
        WriteLog("Terminating application!")
        WriteLog("CloseWriter")
        Return BooleanVal
        Application.Exit()
    End Function
    'Validates computer computer account
    Public Function ValidateComputer(ByVal strComputer As String) As Boolean
        Dim oPrincipalContext As New PrincipalContext(ContextType.Domain, Nothing, Nothing, ContextOptions.SimpleBind, Nothing, Nothing)
        Dim oComputerPrincipal As ComputerPrincipal = ComputerPrincipal.FindByIdentity(oPrincipalContext, strComputer)

        If oComputerPrincipal Is Nothing Then
            ValidateComputer = False
        Else
            ValidateComputer = True
            oComputerPrincipal.Dispose()
        End If
        Return ValidateComputer
    End Function

    'Passes text in to get written to log file
    Public Function WriteLog(ByVal strLogInfo As String) As Boolean

        Using objTextFile As New StreamWriter(StrLogFile, True)
            objTextFile.WriteLine(strLogInfo)
        End Using
        WriteLog = True
    End Function
End Class
